package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.ClipboardManager;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;

public class EaseChatRowText extends EaseChatRow{

	private TextView contentView;
	private PopupWindow typePopupWindow;
	private String copyContent;
	private float scale;
	ClipboardManager clipboardManager ;
	
    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
		super(context, message, position, adapter);
	}

	@Override
	protected void onInflatView() {
		inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
				R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);
	}

	@Override
	protected void onFindViewById() {
		contentView = (TextView) findViewById(R.id.tv_chatcontent);
	}

    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);
        contentView.setOnLongClickListener(myCopyLongClickListener);
        handleTextMessage();
        scale = context.getResources().getDisplayMetrics().density;  
        creatPop();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
            case CREATE: 
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
                break;
            case FAIL:
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case INPROGRESS:
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                break;
            default:
               break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
        
    }
    
    
    private void creatPop() {
    	TextView mPopupText = new TextView(context);
    	
    	mPopupText.setOnClickListener(new OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				if (clipboardManager == null) {
					clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
				}
				if (TextUtils.isEmpty(copyContent)) {
					Toast.makeText(v.getContext(), "复制的内容不能为空", Toast.LENGTH_SHORT).show();
				}else{
					clipboardManager.setText(copyContent);
					Toast.makeText(v.getContext(), "复制成功", Toast.LENGTH_SHORT).show();
				}
				typePopupWindow.dismiss();
			}
		});
		mPopupText.setBackgroundResource(R.mipmap.copy_bg);
		mPopupText.setTextColor(Color.WHITE);
		mPopupText.setPadding(0, 0, 0, 15);
		mPopupText.setText("复制");
		mPopupText.setGravity(Gravity.CENTER);
    	typePopupWindow = new PopupWindow(mPopupText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
    	typePopupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

    private OnLongClickListener myCopyLongClickListener = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			
			int[] location = new int[2]; 
			v.getLocationOnScreen(location); 
			
			TextView tv_copy = (TextView) v ;
			int height = tv_copy.getHeight();
			int width = tv_copy.getWidth();
			typePopupWindow.showAsDropDown(v,(int) (width/2-40*scale), (int) (-height-25*scale));
			
			copyContent = tv_copy.getText().toString().trim();
			return false;
		}
		
	};
}
